package com.yuke.cloud.service.pmc.service.impl;

import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.service.api.opc.dto.UploadFile;
import com.yuke.cloud.service.api.opc.service.OpcFileFeignApi;
import com.yuke.cloud.service.pmc.dao.ProdBrandMapper;
import com.yuke.cloud.service.pmc.dto.ProdBrandDto;
import com.yuke.cloud.service.pmc.entity.ProdBrand;
import com.yuke.cloud.service.pmc.entity.ProdCategory;
import com.yuke.cloud.service.pmc.service.ProdBrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * Created by zys on 2018/09/28.
 */
@Slf4j
@Service
@Transactional
public class ProdBrandServiceImpl extends BaseService<ProdBrand> implements ProdBrandService {
    @Resource
    private ProdBrandMapper prodBrandMapper;
    @Resource
    private OpcFileFeignApi opcFileFeignApi;

    @Override
    public int save(ProdBrand record) {
        record.setBrandId(generateId());
        return super.save(record);
    }

    @Override
    public int update(ProdBrand entity) {
        int count = super.update(entity);
        if (count > 0 && entity.getOldLogoUri() != null && !"".equals(entity.getOldLogoUri())) {
            UploadFile uploadFile = new UploadFile();
            List<String> filePaths = new ArrayList<>();
            filePaths.add(entity.getOldLogoUri());
            uploadFile.setFilePaths(filePaths);
            opcFileFeignApi.filesDelete(uploadFile);
        }
        return count;
    }

    @Override
    public void deleteByIds(String ids) {
        List<ProdBrand> prodBrands = prodBrandMapper.selectByIds(ids);
        super.deleteByIds(ids);
        UploadFile uploadFile = new UploadFile();
        List<String> filePaths = new ArrayList<>();
        for (ProdBrand prodBrand : prodBrands) {
            filePaths.add(prodBrand.getOldLogoUri());
        }
        if (filePaths.size() > 0) {
            uploadFile.setFilePaths(filePaths);
            opcFileFeignApi.filesDelete(uploadFile);
        }
    }

    @Override
    public List<ProdBrandDto> getListByCondition(ProdBrand prodBrand) {
        return prodBrandMapper.getListByCondition(prodBrand);
    }

    @Override
    public List<ProdBrandDto> getEnableListByCondition(ProdBrand prodBrand) {
        return prodBrandMapper.getEnableListByCondition(prodBrand);
    }

    @Override
    public List<ProdCategory> getCategory(Long brandId) {
        List<ProdCategory> category = prodBrandMapper.getCategory(brandId);
        Stream<ProdCategory> stream = category.stream().collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(ProdCategory::getCategoryId)))).stream();
        List<ProdCategory> collect = stream.collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<ProdBrand> getBrandByCategoryIds(Long categoryId) {

        return prodBrandMapper.getBrandByCategoryIds(categoryId);
    }

    @Override
    public List<ProdBrand> getProdBrandByCategoryIds(Long categoryId) {

        return prodBrandMapper.getProdBrandByCategoryIds(categoryId);
    }

    @Override
    public List<ProdBrand> selectBrandByCategoryId(Long categoryId) {

        return prodBrandMapper.selectBrandByCategoryId(categoryId);
    }
}
